(function (exports) {

    var $ = null;
    var question = [];
    var QuestionLayer = cc.Layer.extend({

        onEnter: function () {
            cc.Layer.prototype.onEnter.call(this);
        },
        onCCSLoadFinish: function () {
            var that = this;
        },
        onExit: function () {
            cc.Layer.prototype.onExit.call(this);
        },
        ctor: function (num) {
            this._super();

            var that = this;

            loadCCSTo(res.QuestionLayer_json, this, "Layer");
            $ = create$(this.getChildByName("Layer"));
            shipei($);

            TouchUtils.setOnclickListener($('root.bg.checkA'), function (node) {
                $('root.bg.checkA').setSelected(true);
                $('root.bg.checkB').setSelected(false);
                $('root.bg.checkC').setSelected(false);
                $('root.bg.checkD').setSelected(false);
            });
            TouchUtils.setOnclickListener($('root.bg.checkB'), function (node) {
                $('root.bg.checkA').setSelected(false);
                $('root.bg.checkB').setSelected(true);
                $('root.bg.checkC').setSelected(false);
                $('root.bg.checkD').setSelected(false);
            });
            TouchUtils.setOnclickListener($('root.bg.checkC'), function (node) {
                $('root.bg.checkA').setSelected(false);
                $('root.bg.checkB').setSelected(false);
                $('root.bg.checkC').setSelected(true);
                $('root.bg.checkD').setSelected(false);
            });
            TouchUtils.setOnclickListener($('root.bg.checkD'), function (node) {
                $('root.bg.checkA').setSelected(false);
                $('root.bg.checkB').setSelected(false);
                $('root.bg.checkC').setSelected(false);
                $('root.bg.checkD').setSelected(true);
            });
            TouchUtils.setOnclickListener($('root.bg.btn_confirm'), function (node) {
                that.index ++;
                if(that.index >= question.length) {
                    that.removeFromParent();
                }else{
                    if($('root.bg.checkA').isSelected() == false && $('root.bg.checkB').isSelected() == false &&
                        $('root.bg.checkC').isSelected() == false && $('root.bg.checkD').isSelected() == false){
                        showToast('请选择一个选项');
                        return;
                    }
                    if(($('root.bg.checkA').isSelected() && question[that.index]['answer'] != 0) ||
                        ($('root.bg.checkB').isSelected() && question[that.index]['answer'] != 1) ||
                        ($('root.bg.checkC').isSelected() && question[that.index]['answer'] != 2) ||
                        ($('root.bg.checkD').isSelected() && question[that.index]['answer'] != 3) ){
                        showToast('错误');
                    }
                    that.scheduleOnce(function(){
                        $('root.bg.checkA').setSelected(false);
                        $('root.bg.checkB').setSelected(false);
                        $('root.bg.checkC').setSelected(false);
                        $('root.bg.checkD').setSelected(false);
                        $('root.bg.content').setString("单词："+question[that.index]['ti']+"的中文意思是以下答案中的哪一个？");
                        $('root.bg.answerA').setString(question[that.index]['choose'][0]);
                        $('root.bg.answerB').setString(question[that.index]['choose'][1]);
                        $('root.bg.answerC').setString(question[that.index]['choose'][2]);
                        $('root.bg.answerD').setString(question[that.index]['choose'][3]);
                    }, 1.5);
                }
            });

            question = [];
            this.index = 0;
            this.initUI(num);
        },
        initUI: function(qnum){
            for(var i=0;i<qnum;i++){
                var index = parseInt(Math.random() * sijiWord.length);
                var ti = sijiWord[index][0];
                var choose = [];
                var daan = sijiWord[index][1];
                for(var j=0;j<sijiWord.length;j++){
                    if(choose.length < 3) {
                        var tmp = parseInt(Math.random() * sijiWord.length);
                        if (tmp != index && choose.indexOf(sijiWord[tmp][1]) < 0) choose.push(sijiWord[tmp][1]);
                    }else{
                        continue;
                    }
                }
                //选项打乱
                var answer = parseInt(Math.random() * 4);
                if(answer == 3){
                    choose[3] = daan;
                }else{
                    for(var n=3;n>answer;n--){
                        choose[n] = choose[n-1];
                    }
                    choose[answer] = daan;
                }
                question.push({'ti': ti, 'choose': choose, 'answer': answer});
            }
            // console.log(question);
            $('root.bg.checkA').setSelected(false);
            $('root.bg.checkB').setSelected(false);
            $('root.bg.checkC').setSelected(false);
            $('root.bg.checkD').setSelected(false);
            $('root.bg.content').setString("单词："+question[this.index]['ti']+"的中文意思是以下答案中的哪一个？");
            $('root.bg.answerA').setString(question[this.index]['choose'][0]);
            $('root.bg.answerB').setString(question[this.index]['choose'][1]);
            $('root.bg.answerC').setString(question[this.index]['choose'][2]);
            $('root.bg.answerD').setString(question[this.index]['choose'][3]);
        }
    });
    exports.QuestionLayer = QuestionLayer;
})(window);
